package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilterMobile {
    /**
     * 手机号码中拨打5次，手机号前几位为虚拟号码判断
     */
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-mobile.csv";
        String mothodname = "mobile";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,5);
    }
    @Test(description = "测试用户信息中前5个人为手机号为虚拟号码。第六个为被拨打5次，预期结果只返回232110006")
    public void checkMobileIsVirtualNumberOrCalledTimesGreaterFive(){
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        Assert.assertEquals(resurlt.size(),1);
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(232110006));
        Assert.assertEquals(msg,"success");
    }
    @Test(description = "请求参数cuIds为空的情况下，接口返回结果code为100，msg为param cuIds must be not null，data为空")
    public void paramNull(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",null);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"100");
    }
    @Test(description = "请求参数cuIds为一个用户切用户信息正常时，返回该用户本身的cuid")
    public void checkOnlyOneCuerId(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds","232110006");
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(232110006));
        Assert.assertEquals(code,"200");
    }
    @Test(description = "参数cuIds为一个用户信息正常，其中用户232110007未拨打5次的用户,预期返回数据的size为1，code为0，返回用户信息232110006")
    public void checkMobileIsCalledTimesGreaterFive(){
        HashMap hashMap = new HashMap();
        String cuIds="[232110007,232110006]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),1);
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(232110006));
        Assert.assertEquals(code,"200");
    }

    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
//        operationRedis.clearRedisCallTimes();//清空特殊的值
//      String s=  operationRedis.getValue();
//        System.out.println(s);

    }





}
